package com.kevin.util.tag;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class DateFormat  extends TagSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String date;
	private String outtype;
	private int intype=-1;
	

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}		

	public String getOuttype() {
		return outtype;
	}

	public void setOuttype(String outtype) {
		this.outtype = outtype;
	}

	public int getIntype() {
		return intype;
	}

	public void setIntype(int intype) {
		this.intype = intype;
	}

	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		try {
			SimpleDateFormat fmt = null;
			SimpleDateFormat infmt = null;
			if(intype == 0){
				infmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			}else if(intype == 1){
				infmt = new SimpleDateFormat("yyyy-MM-dd");
			}else if(intype == 2){
				infmt = new SimpleDateFormat("hh:mm:ss");
			}else {
				infmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			}
			Date d = infmt.parse(date);
			if(outtype.equals("date"))
				fmt = new SimpleDateFormat("yyyy年 MM 月 dd 日");
			else if(outtype.equals("time"))
				fmt = new SimpleDateFormat("hh 时 mm 分 ss 秒");
			else if(outtype.equals("all"))
				fmt = new SimpleDateFormat("yyyy 年 MM 月 dd 日 hh 时 mm 分 ss 秒");
			else 
				fmt = new SimpleDateFormat(outtype);
			pageContext.getOut().print(fmt.format(d));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		return EVAL_PAGE;
	}
	

}
